﻿// DlgRvaRaw.cpp: 实现文件
//

#include "pch.h"
#include "afxdialogex.h"
#include "DlgRvaRaw.h"
#include "PE_Studio.h"
#include "PE_StudioDlg.h"

CPEStudioDlg* parent;

// DlgRvaRaw 对话框

IMPLEMENT_DYNAMIC(DlgRvaRaw, CDialogEx)

DlgRvaRaw::DlgRvaRaw(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DLG_RVARAW, pParent)
	, m_nRadioBtnGroup(1)		//默认选中第2个按钮，即IDC_RADIO_RVA
	, m_EditFOA(_T(""))
	, m_EditRVA(_T(""))
	, m_EditVA(_T(""))
	, m_EditSection(_T(""))
{
}

DlgRvaRaw::~DlgRvaRaw()
{
}

void DlgRvaRaw::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Radio(pDX, IDC_RADIO_FOA, m_nRadioBtnGroup);
	DDX_Text(pDX, IDC_EDIT_FOA, m_EditFOA);
	DDX_Text(pDX, IDC_EDIT_RVA, m_EditRVA);
	DDX_Text(pDX, IDC_EDIT_VA, m_EditVA);
	DDX_Text(pDX, IDC_EDIT_SECTION, m_EditSection);
}


BEGIN_MESSAGE_MAP(DlgRvaRaw, CDialogEx)
	ON_BN_CLICKED(IDC_BTN_CALC, &DlgRvaRaw::OnBnClickedCalc)
	ON_BN_CLICKED(IDC_RADIO_FOA, &DlgRvaRaw::OnBnClickedRadioFoa)
	ON_BN_CLICKED(IDC_RADIO_RVA, &DlgRvaRaw::OnBnClickedRadioRva)
	ON_BN_CLICKED(IDC_RADIO_VA, &DlgRvaRaw::OnBnClickedRadioVa)
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()


// DlgRvaRaw 消息处理程序


void DlgRvaRaw::OnBnClickedCalc()
{
	// TODO: 在此添加控件通知处理程序代码
	DWORD dwFOA = 0, dwRVA = 0, dwVA = 0;
    CString szBuffer;
	CPEStudioDlg* pHwndParent = (CPEStudioDlg*)(AfxGetApp()->m_pMainWnd);	// 父窗口句柄
    
	UpdateData(TRUE);
	int nCurSel = GetCheckedRadioButton(IDC_RADIO_FOA, IDC_RADIO_VA);
	switch (nCurSel)
	{
        case IDC_RADIO_FOA:			
			dwFOA = _tcstoul(m_EditFOA, NULL, 16);
			dwRVA = pHwndParent->m_pPE->FOAtoRVA(dwFOA);
			dwVA = dwRVA + pHwndParent->m_pPE->m_pOptionalHeader->ImageBase;
			break;
        case IDC_RADIO_RVA:
			dwRVA = _tcstoul(m_EditRVA, NULL, 16);
			dwFOA = pHwndParent->m_pPE->RVAtoFOA(dwRVA);
			dwVA = dwRVA + pHwndParent->m_pPE->m_pOptionalHeader->ImageBase;
            break;
            
        case IDC_RADIO_VA:
			dwVA = _tcstoul(m_EditVA, NULL, 16);
			dwRVA = dwVA - pHwndParent->m_pPE->m_pOptionalHeader->ImageBase;				
			dwFOA = pHwndParent->m_pPE->RVAtoFOA(dwRVA);
            break;
		default:
			break;
	}
	if (dwRVA != 0)
	{
		for (int i = 0; i < pHwndParent->m_pPE->m_vecSectionTable.size(); i++)
		{
			if (dwFOA >= pHwndParent->m_pPE->m_vecSectionTable[i].PointerToRawData && dwFOA <= pHwndParent->m_pPE->m_vecSectionTable[i].PointerToRawData + pHwndParent->m_pPE->m_vecSectionTable[i].SizeOfRawData)
			{
				//wsprintf(szBuffer, _T("%s"), (CString)(pHwndParent->m_pPE->m_vecSectionTable[i].Name));
				szBuffer = pHwndParent->m_pPE->m_vecSectionTable[i].Name;
			}
		}
	}
	m_EditFOA.Format(_T("%08X"), dwFOA);
	m_EditRVA.Format(_T("%08X"), dwRVA);
	m_EditVA.Format(_T("%08X"), dwVA);
	m_EditSection = szBuffer;
	UpdateData(FALSE);
    
}


void DlgRvaRaw::OnBnClickedRadioFoa()
{
	// TODO: 在此添加控件通知处理程序代码

	GetDlgItem(IDC_EDIT_FOA)->SetFocus();
	SendDlgItemMessage(IDC_EDIT_FOA, EM_SETREADONLY, FALSE, 0);
	SendDlgItemMessage(IDC_EDIT_RVA, EM_SETREADONLY, TRUE, 0);
	SendDlgItemMessage(IDC_EDIT_VA, EM_SETREADONLY, TRUE, 0);
}


void DlgRvaRaw::OnBnClickedRadioRva()
{
	// TODO: 在此添加控件通知处理程序代码
	GetDlgItem(IDC_EDIT_RVA)->SetFocus();
	SendDlgItemMessage(IDC_EDIT_FOA, EM_SETREADONLY, TRUE, 0);
	SendDlgItemMessage(IDC_EDIT_RVA, EM_SETREADONLY, FALSE, 0);
	SendDlgItemMessage(IDC_EDIT_VA, EM_SETREADONLY, TRUE, 0);
}


void DlgRvaRaw::OnBnClickedRadioVa()
{
	// TODO: 在此添加控件通知处理程序代码
	GetDlgItem(IDC_EDIT_VA)->SetFocus();
	SendDlgItemMessage(IDC_EDIT_FOA, EM_SETREADONLY, TRUE, 0);
	SendDlgItemMessage(IDC_EDIT_RVA, EM_SETREADONLY, TRUE, 0);
	SendDlgItemMessage(IDC_EDIT_VA, EM_SETREADONLY, FALSE, 0);
}


BOOL DlgRvaRaw::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化	
	m_EditFOA = _T("");
	m_EditRVA = _T("");
	m_EditVA = _T("");
	m_EditSection = _T("");

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


// 让对话框一加载完成后某个控件就可以获取到焦点；以及设置3个编辑文本控件的读写状态。
LRESULT DlgRvaRaw::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: 在此添加专用代码和/或调用基类
	switch (message)
	{
	case WM_PAINT:
		CheckRadioButton(IDC_RADIO_FOA, IDC_RADIO_VA, IDC_RADIO_RVA);	//默认选中第2个按钮，即IDC_RADIO_RVA
		SendDlgItemMessage(IDC_EDIT_FOA, EM_SETREADONLY, TRUE, 0);
		SendDlgItemMessage(IDC_EDIT_RVA, EM_SETREADONLY, FALSE, 0);
		SendDlgItemMessage(IDC_EDIT_VA, EM_SETREADONLY, TRUE, 0);
		GetDlgItem(IDC_EDIT_RVA)->SetFocus();

		break;
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(LOWORD(wParam));
			return 0;
		}
		break;
	case WM_CLOSE:
		EndDialog(0);
		break;
	default:
		break;
	}
	return CDialogEx::WindowProc(message, wParam, lParam);
}

BOOL DlgRvaRaw::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
    if (pMsg->wParam == VK_RETURN)
    {
        SendDlgItemMessage(IDC_BTN_CALC, BM_CLICK, 0, 0);
        return true;
    }
	return CDialogEx::PreTranslateMessage(pMsg);
}
